#!/usr/bin/env bash
# dashboard-snapshot-config -- Manipulate report templates
# > dashboard-snapshot-config ls
# > dashboard-snapshot-config get NAME
# > dashboard-snapshot-config put NAME <CONFIG_JSON
# > dashboard-snapshot-config delete NAME
# > dashboard-snapshot-config generate NAME
#
# Author: Jaeho Shin <netj@cs.stanford.edu>
# Created: 2015-05-01
set -eu

DEEPDIVE_APP=$(find-deepdive-app)
export DEEPDIVE_APP
cd "$DEEPDIVE_APP"

[[ $# -gt 0 ]] || usage "$0" "No verb given"
Command=$1; shift
case $Command in
    ls)
        ;;
    get|put|delete|generate)
        ConfigName=${1:-}; shift || usage
        ConfigFile=snapshot-"$ConfigName".conf
        ;;
    *)
        usage "$0" "ls, get, put, or delete must be given"
esac

case $Command in
    ls)
        echo "["
        for ConfigFile in snapshot-*.conf; do
            [[ -e "$ConfigFile" ]] || continue
            ConfigName=${ConfigFile#snapshot-}
            ConfigName=${ConfigName%.conf}
            echo ","
            # TODO handle JSON escapes correctly
            printf '"%s"\n' "$ConfigName"
        done | tail -n +2
        echo "]"
        ;;

    get) # encode snapshot configuration into a JSON object
        [[ -e "$ConfigFile" ]] || error "$ConfigName: No such snapshot configuration"
        export JSON_FILE=-  # to use report-values for constructing JSON

        # get ready to interpret the 
        report() {
            local name=$1; shift
            echo ","
            report-values \
                reportTemplate="$name" \
                params="$(report-values "$@")"
        }
        section() {
            : # ignored
        }
        echo "["
        (source "$ConfigFile") | tail -n +2
        echo "]"
        ;;

    put) # decode snapshot configuration from given JSON object
        cat | # XXX a workaround for ENXIO error coming from using /dev/stdin from nested nodejs processes
        coffee -e '
            fs = require "fs"
            esc = require "shell-escape"
            _ = require "underscore"
            [jsonFile] = process.argv[4..]

            ## read and parse input report template object
            snapshotConfig = JSON.parse fs.readFileSync jsonFile

            ## write files under the report template file
            for {reportTemplate, params} in snapshotConfig
                console.log esc ["report", reportTemplate, (
                        "#{name}=#{value}" for name,value of params
                    )...]
        ' /dev/stdin >"$ConfigFile".tmp
        mv -f "$ConfigFile".tmp "$ConfigFile"
        ;;

    delete) # delete given snapshot configuration
        rm -f "$ConfigFile"
        ;;

    generate) # derive a default snapshot configuration from DeepDive's application.conf
        ! [[ -e "$ConfigFile" ]] ||
            error "$ConfigFile: snapshot configuration $ConfigName already exists"
        eval-coffee-on-deepdive-app-conf '
            esc = require "shell-escape"

            console.log "report corpus   table=sentences"
            console.log "report learning"
            for table,columns of appConf.deepdive?.schema?.variables ? {}
                for column,variableSchema of columns
                    console.log esc ["report", "variable", "variable=#{table}.#{column}"]
        ' >"$ConfigFile".tmp
        mv -f "$ConfigFile".tmp "$ConfigFile"
        ;;
esac
